package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.Familys;
import com.gx.mxbbx.po.SysUser;
import com.gx.mxbbx.service.FamilysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class FamilysController {
    @Autowired
    private FamilysService familysService;

    @RequestMapping("/getfamilysList")
    public String getfamilysList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String countyId = request.getParameter("countyIds");
        String familyId = request.getParameter("contexts");
        String familyName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<Familys> pageInfo = familysService.getfamilysList(countyId, familyId, familyName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", familyName);
        model.addAttribute("contexts", familyId);
        return "/family/family_list";
    }

    @RequestMapping("/look")
    public String look(Integer id, Model model) {
        Familys familys = familysService.getlook(id);
        model.addAttribute("familys", familys);
        return "/family/family";
    }

    @RequestMapping("/openfamilyAdd")
    public String openfamilyAdd(HttpSession session, Model model) {
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
        model.addAttribute("sysUser", sysUser);
        return "/family/family_add";
    }

    @RequestMapping("/familyadd")
    public String familyadd(Familys familys, HttpServletRequest request, Model model) {

        familysService.familyadd(familys);
        return getfamilysList(1, 10, request, model);
    }

    @RequestMapping("/familysopenEdit")
    public String familysopenEdit(Integer id, Model model) {
        Familys familys = familysService.familysopenEdit(id);
        model.addAttribute("familys", familys);
        return "/family/family_edit";
    }

    @RequestMapping("/familysedit")
    public String familysedit(Familys familys, HttpServletRequest request, Model model) {
        familysService.familysedit(familys);
        return getfamilysList(1, 10, request, model);
    }

    @RequestMapping("/familydelete")
    public String familydelete(Integer id, HttpServletRequest request, Model model) {
        familysService.familydelete(id);
        return getfamilysList(1, 10, request, model);
    }
}
